package com.dalei.vdsmanager.management.vds.action;

import java.util.Date;

import com.dalei.vdsmanager.management.vds.dao.VDSUser;
import com.dalei.vdsmanager.management.vds.service.VDSUserService;
import com.opensymphony.xwork2.ActionSupport;

public class AddVDSUserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5817571371696649989L;

	private VDSUserService vdsUserService;
	
	private String userName;
	
	private String password;
	
	private String repeatPassword;
	
	public VDSUserService getVdsUserService() {
		return vdsUserService;
	}

	public void setVdsUserService(VDSUserService vdsUserService) {
		this.vdsUserService = vdsUserService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepeatPassword() {
		return repeatPassword;
	}

	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		// return super.execute();
		String user = userName.trim();
		VDSUser vdsUser = new VDSUser();
		vdsUser.setUser(user);
		vdsUser.setPassword(password);
		vdsUser.setCreatedTime(new Date());
		vdsUserService.addVDSUser(vdsUser);
		return SUCCESS;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		super.validate();
		clearFieldErrors();
		String usr = userName.trim();
		if ((usr.length() == 0) || (usr.length() > 64)) {
			addFieldError("userName", getText("addVDSUser.userName.invalid"));
		}
		if (vdsUserService.checkRepeat(usr)) {
			addFieldError("userName", getText("addVDSUser.userName.existed"));
		}
		if (!repeatPassword.equals(password)) {
			addFieldError("repeatPassword", getText("addVDSUser.repeatPassword.notMatch"));
		}
	}
}
